import Mock from 'mockjs'
import { param2Obj } from '@/utils'

const List = []
const count = 20

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    name: '@csentence(3, 5)',
    province: '@province',
    city: '@city',
    county: '@county',
    address: '@csentence(3, 5)',
    contain: '@integer(100,20000)',
    area: '@integer(100,2000)',
    state: '@integer(0, 1)'
  }))
}

export default {
  getList: config => {
    const { page = 1, limit = 20, sort, state } = param2Obj(config.url)

    let mockList = List.filter(item => {
      if (state && item.state !== Number(state)) return false
      return true
    })
    if (sort === '-id') {
      mockList = mockList.reverse()
    }

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
    return {
      total: mockList.length,
      items: pageList
    }
  },
  create: () => ({
    data: 'success'
  }),
  update: () => ({
    data: 'success'
  })
}
